SAuthModule = angular.module("SAuthModule", [ 'ui.router',	'SellerModule' ]);

SAuthModule.config(function($stateProvider, $urlRouterProvider) {

	$urlRouterProvider.otherwise("/login");
	$stateProvider.state('login', {
		url : "/login",
		templateUrl : 'authentication/login.html',
		controller : 'supLoginCtrl'
	});

	$stateProvider.state('recover-password', {
		url : "/recover-password",
		templateUrl : 'authentication/recover-password.html',
		controller : 'supRecoverPasswordCtrl'
	});

	$stateProvider.state('create-account', {
		url : '/create-account',
		templateUrl : 'authentication/create-account.html',
		controller : 'supCreateAccountCtrl'
	});

	$stateProvider.state('account-created', {
		url : "/account-created",
		templateUrl : 'authentication/account-created.html',
		controller : 'emptyCtrl'
	});

	$stateProvider.state('password-recovered', {
		url : "password-recovered",
		templateUrl : 'authentication/password-recovered.html',
		controller : 'emptyCtrl'
	});

});

/*
 * SAuthModule.factory('Seller', function($resource) {
 * 
 * return $resource("/api/Seller/:id/:nameOfAttribute", { id : '@id',
 * nameOfAttribute : "" }, { email : { method : 'GET', params : {
 * nameOfAttribute : "email" } } });
 * 
 * });
 */

SAuthModule.controller('supLoginCtrl', function($scope, $window, Seller) {
	$window.Seller = Seller;
	$window.Seller = Seller
	$window.$s = $scope;
	$scope.loginInfo = {
		email : "",
		password : ""
	};

	$scope.signIn = function() {
		Seller.login(undefined, $scope.loginInfo, function(data) {
			sessionStorage.setItem("idSeller", data.data);
			$window.location.href = "/seller/";
		})

	}

});
SAuthModule.controller('supCreateAccountCtrl',
		function($scope, $state, Seller) {
			$scope.personForm = true;
			$scope.personInfo = {
				firstName : "",
				lastName : "",
				email : "",
				birthDate : new Date(),
				mobile : "",
				address : "",
				password : "",
				repeatPassword : "",
				terms : false,

			}

			$scope.errorMessage = "";

			$scope.signUp = function() {

				Seller.register({}, $scope.personInfo, function() {
					$state.go("account-created");
				}, function(error) {
					$scope.errorMessage = error;
				})

			}
		});

SAuthModule.controller('SellerAuthCtrl', function($scope, $http, $location) {

	window.fbAsyncInit = function() {
		window.fbAsyncInit = undefined;
		FB.init({
			appId : '1393689537528345',
			status : false
		});
		// $scope.login();
	};

	document.querySelector("#fbsdk").setAttribute("src",
			"//connect.facebook.net/en_US/all.js");

});
SAuthModule.controller('supRecoverPasswordCtrl', function($scope, $http,
		$location) {
	$scope.email = "";
	$scope.recoverPassword = function() {
		$http.post("/users/recoverPassword", $scope.email).then(function() {
			$location.path("/password-recovered");
		}, function(info) {
			$scope.errorMessage = info;
		});

	}
});

SAuthModule.controller('emptyCtrl', function() {
});

/*
 * FB.getLoginStatus(function(data) { console.log(data); $http({ method :
 * 'POST', url : '/login', data : data }).then(function() { $scope.user = data;
 * });
 * 
 * });
 */